﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models.DataModel;
using System.Data.Entity;
using Models.AddFunction;
using System.Security.Cryptography;
namespace Models.DataStore
{
    public partial class Ctrl : Core
    {
        public bool Login(string username, string password)
        {
            bool result = false;
            UserAccount getUser = new UserAccount();
            getUser = db.UserAccounts.Where(u => u.UserName == username).FirstOrDefault();
            try
            {
                if (getUser.Password == MD5Hash(password))
                    result = true;
            }
            catch
            {
                result = false;
            }
            return result;
        }
        public async Task<int> AddUser(UserAccount objUser)
        {
            if (!string.IsNullOrEmpty(objUser.UserName))
            {
                objUser.Password = MD5Hash(objUser.Password);
                objUser.CreatedOn = DateTime.Now;
                objUser.ModifiedOn = DateTime.Now;
                objUser.UserAccountID = Guid.NewGuid();
                db.UserAccounts.Add(objUser);
                return await db.SaveChangesAsync();
            }
            return 0;
        }
        public async Task<int> EditUser(UserAccount objUser)
        {
            try
            {
                objUser.ModifiedOn = DateTime.Now;
                //objUser.Password = MD5Hash(objUser.Password);
                db.Entry(objUser).State = EntityState.Modified;
                return await db.SaveChangesAsync();
            }
            catch
            {
                return 0;
            }
        }
        public async Task<int> ChangPassUser(UserAccount objUser)
        {
            try
            {
                UserAccount acc = db.UserAccounts.FirstOrDefault(c => c.UserAccountID == objUser.UserAccountID);
                acc.Password = MD5Hash(objUser.Password);
                db.Entry(acc).Property(x => x.Password).IsModified = true;
                return await db.SaveChangesAsync();
            }
            catch
            {
                return 0;
            }
        }
        public int CheckPassUser(UserAccount objUser, string OldPassword)
        {
            try
            {
                UserAccount acc = db.UserAccounts.FirstOrDefault(c => c.UserAccountID == objUser.UserAccountID);
                if (acc.Password == MD5Hash(OldPassword))
                    return 1;
                return 0;
            }
            catch
            {
                return 0;
            }
        }
        public async Task<int> DeleteUser(Guid? UserId)
        {
            UserAccount objUser = db.UserAccounts.FirstOrDefault(c => c.UserAccountID == UserId);
            db.UserAccounts.Remove(objUser);
            return await db.SaveChangesAsync();
        }
        public IQueryable<UserAccount> GetlstUserAccount()
        {
            var LstUserAccount = db.UserAccounts;
            return LstUserAccount;
        }
        public UserAccount GetObjUser(Guid UserId)
        {
            var Lstuser = db.UserAccounts.FirstOrDefault(c => c.UserAccountID == UserId);
            return Lstuser;
        }
        public UserAccount GetObjUserByUsername(string username)
        {
            var Lstuser = db.UserAccounts.FirstOrDefault(c => c.UserName == username);
            return Lstuser;
        }
        public GroupUser GetobjGroupUserById(Guid? groupId)
        {
            var ObjgroupId = db.GroupUsers.FirstOrDefault(c => c.GroupUserID == groupId);
            return ObjgroupId;
        }
        public static string MD5Hash(string text)
        {
            MD5 md5 = new MD5CryptoServiceProvider();

            //compute hash from the bytes of text
            md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(text));

            //get hash result after compute it
            byte[] result = md5.Hash;

            StringBuilder strBuilder = new StringBuilder();
            for (int i = 0; i < result.Length; i++)
            {
                //change it into 2 hexadecimal digits
                //for each byte
                strBuilder.Append(result[i].ToString("x2"));
            }

            return strBuilder.ToString();
        }
    }
}
